﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using PS3Helpers;

namespace PS3Packager
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            string[] args = Environment.GetCommandLineArgs();

            
            MainForm mainfrm = new MainForm();

            if (args.Length > 1)
            {
                string prefix = args[1].ToUpper();

                switch (prefix)
                {
                    case "-BUILD":
                        {
                            if (args.Length >= 2)
                            {
                                PS3PackageFile packagefile = new PS3PackageFile(args[2]);
                                
                                CompilationProgressForm progressfrm = new CompilationProgressForm();

                                PackageGeneratorThread genthread = new PackageGeneratorThread(progressfrm, true, packagefile);
                                progressfrm.StartPosition = FormStartPosition.CenterScreen;
                                progressfrm.ShowDialog(genthread, packagefile);
                            }
                            else
                            {
                                MessageBox.Show("Cannot build. Project filename not specified!", "Error", MessageBoxButtons.OK);
                            }

                            return;
                        }
                    case "-CREATEPROJECT":
                        {
                            if (args.Length < 2)
                            {
                                MessageBox.Show("Cannot create project. Directory not specified!", "Error", MessageBoxButtons.OK);
                                return;
                            }

                            string directory = args[2];
                            if (!Directory.Exists(directory))
                            {
                                MessageBox.Show("Cannot create project. Directory not found!", "Error", MessageBoxButtons.OK);
                                return;
                            }

                            directory = directory.TrimEnd(Path.DirectorySeparatorChar);
                            string dirname = directory.Substring(Path.GetDirectoryName(directory).Length + 1);

                            string filename = directory + Path.DirectorySeparatorChar + dirname + ".p3p";

                            if (!File.Exists(filename))
                            {
                                PS3PackageFile packagefile = new PS3PackageFile();
                                packagefile.Title = dirname;
                                packagefile.ContentId = PS3Helpers.UserControls.ContentIdEditor.GenerateId();


                                packagefile.SaveFile(filename);

                                mainfrm.LoadFile(filename);
                            }

                            break;
                        }
                    default:
                        {
                            mainfrm.LoadFile(args[1]);
                            break;
                        }
                }
            }

            Application.Run(mainfrm);
        }
    }
}
